/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/


#include "newsparser.h"

#include <QList>
#include <QByteArray>

#include <QDebug>

#include "../Types/news.h"

char parseNews(const QByteArray& newsData, int indexData, News& n)
{
    int indexStartTitle = newsData.indexOf("\"headline\">",indexData) + strlen("\"headline\">");
    int indexEndTitle = newsData.indexOf("</span></h2>",indexStartTitle);
    int indexStartText = indexEndTitle + strlen("</span></h2>");
    int indexEndText = newsData.indexOf("<h2>",indexStartText);

    if ( indexEndText == -1 )
    {
        indexEndText = indexStartText;
        while ( newsData.indexOf("</p>",indexEndText) != -1)
        {
            indexEndText = newsData.indexOf("</p>",indexEndText);
            // Allow to go after the matching string
            indexEndText += strlen("</p>");
        }
    }

    n.title = QString::fromUtf8(newsData.mid(indexStartTitle, indexEndTitle - indexStartTitle));

    // Try to parse the data, if available
    if ( n.title.contains("<span class=\"date\">"))
    {
        int indexClosingSpan = n.title.indexOf("</span>");
        int indexDate = n.title.indexOf("<span class=\"date\">",indexClosingSpan) + strlen("<span class=\"date\">");

        n.date = n.title.right(n.title.size()-indexDate);
        n.title = n.title.left(indexClosingSpan);
    }

    n.text = QString::fromUtf8(newsData.mid(indexStartText, indexEndText - indexStartText));

    return 0;
}

bool Parser :: readPageNews(const QByteArray& newsData, QList<News>& news)
{
    int indexNewsData = 0;

    qDebug() << "Parser :: parserNewsPage";

    if ( newsData.contains("Welcome at Nectarine Demoscene Radio! (sticky)") == false )
    {
        qDebug() << "It looks like the news page is not correct";
        return false;
    }

    while ( (indexNewsData = newsData.indexOf("<h2>",indexNewsData)) != -1 )
    {
        News n;

        parseNews(newsData,indexNewsData,n);

        news.push_back(n);

        // Move index to avoid infinite looping
        indexNewsData += strlen("<h2>");
    }

    qDebug() << news;

    return true;
}

bool Parser :: readNbNewPM(const QByteArray& newsData, int& nbPMs)
{
    nbPMs = 0;

    qDebug() << "Parser :: readNbNewPM";

    if ( newsData.contains("My Inbox") == false )
    {
        qDebug() << "It looks like the inbox page is not correct";
        return false;
    }

    int indexStartNbNewPM = newsData.indexOf("My Inbox</a>");
    int indexEndNbNewPM = newsData.indexOf("</li>",indexStartNbNewPM);

    int indexStartNumberPM = newsData.indexOf("(",indexStartNbNewPM);
    if ( indexStartNumberPM != -1 && indexStartNumberPM < indexEndNbNewPM )
    {
        indexStartNumberPM+=1;  // Go just after the '('
        int indexEndNumberPM = newsData.indexOf(")",indexStartNumberPM);
        bool isValid = true;

        nbPMs = newsData.mid(indexStartNumberPM, indexEndNumberPM-indexStartNumberPM).toInt(&isValid);
        return isValid;
    }
    else
    {
        nbPMs = 0;
    }


    return true;
}
